package com.wetao.lib_common.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5工具
 */
public class MD5Utils {
    //静态方法，便于作为工具类
    public static String getMd5(String plainText) {
                try {
                    MessageDigest md = MessageDigest.getInstance("MD5");
                    md.update(plainText.getBytes());
                         byte b[] = md.digest();
                         int i;
                        StringBuffer buf = new StringBuffer("");
                        for (int offset = 0; offset < b.length; offset++) {
                                i = b[offset];
                                 if (i < 0)
                                        i += 256;
                                 if (i < 16)
                                         buf.append("0");
                                 buf.append(Integer.toHexString(i));
                             }
                        //32位加密
                         return buf.toString();
                         // 16位的加密
                         //return buf.toString().substring(8, 24);
                     } catch (NoSuchAlgorithmException e) {
                         e.printStackTrace();
                         return null;
                     }
    }

    private static String bytesToHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(0xFF & bytes[i]);
            if (hex.length() == 1) {
                sb.append('0');
            }
            sb.append(hex);
        }
        return sb.toString();
    }

    public static String hashKey(String key) {
        String hashKey;
        try {
            final MessageDigest mDigest = MessageDigest.getInstance("MD5");
            mDigest.update(key.getBytes());
            hashKey = bytesToHexString(mDigest.digest());
        } catch (NoSuchAlgorithmException e) {
            hashKey = String.valueOf(key.hashCode());
        }
        return hashKey;
    }
}
